package org.firefly.service;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.firefly.entity.TimeCapsule;

import java.time.LocalDateTime;

@Service
public class EmailService {

    private final JavaMailSender mailSender;

    @Value("${spring.mail.username}")
    private String fromEmail;

    public EmailService(JavaMailSender mailSender) {
        this.mailSender = mailSender;
    }

    public void sendCapsuleNotification(TimeCapsule capsule) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("萤窗时间胶囊 <" + fromEmail + ">");
        message.setTo(capsule.getRecipientEmail());
        message.setSubject("您的时间胶囊已开启：" + capsule.getTitle());

        String text = "亲爱的用户：\n\n" +
                "您的时间胶囊 \"" + capsule.getTitle() + "\" 已开启。\n\n" +
                "内容：" + capsule.getContent() + "\n\n" +
                "创建时间：" + capsule.getCreationDate() + "\n" +
                "开启时间：" + LocalDateTime.now() + "\n\n" +
                "来自萤窗时间胶囊";

        message.setText(text);
        mailSender.send(message);
    }
}